package com.fpx.xms.api.rabbitmq.consumer;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;

import com.fpx.open.platform.service.InvokeLogService;
import com.fpx.xms.api.base.model.InvokeLog;
import com.fpx.xms.api.rabbitmq.converter.FastJsonMessageConverter;
import com.rabbitmq.client.Channel;

public class InvokeLogConsumer implements ChannelAwareMessageListener{
	
	private final Logger logger = LoggerFactory.getLogger(getClass());
	
	@Resource
	private InvokeLogService invokeLogService ;
	
	@Resource
	private FastJsonMessageConverter msgConverter ;

	@Override
	public void onMessage(Message message, Channel channel) throws Exception {	
		long deliveryTag = message.getMessageProperties().getDeliveryTag();
		boolean isReject = true ;
		try {
			InvokeLog invokeLog = msgConverter.fromMessage(message, new InvokeLog());
			logger.info("Add invoke log to db , the customerOrderNo is "+invokeLog.getCustomerOrderNo());
			invokeLogService.addInvokeLog(invokeLog);			
			channel.basicAck(deliveryTag, false);
			isReject = false ;
		} catch (Exception e) {
            logger.error("add invoke log failure",e);
		}finally{
			if(isReject){
			    channel.basicReject(deliveryTag, false);
			}
		}
	}

}
